home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dcc / subs.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  3KB  |  215 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  SUBS.C
  9.  */
  10.  
  11. #include "defs.h"
  12. #ifdef AMIGA
  13. #include <exec/execbase.h>
  14. #endif
  15.  
  16. Prototype int ExtArgsEnv(short, char ***, char *);
  17. Prototype int ExtArgsFile(short, char ***, char *);
  18. Prototype int ExtArgsBuf(short, char ***, char *, long);
  19.  
  20. Prototype char *skipspace(char *);
  21. Prototype char *skipnspace(char *);
  22. Prototype void CreateObjPath(char *);
  23.  
  24. #ifdef AMIGA
  25. extern struct ExecBase *SysBase;
  26. #endif
  27.  
  28. /*
  29.  *  ExtArgsEnv()    DCCOPTS
  30.  */
  31.  
  32. #ifdef AMIGA
  33.  
  34. int
  35. ExtArgsEnv(short ac, char ***pav, char *envname)
  36. {
  37.     long len = -1;
  38.     char *str;
  39.     int nac = 0;
  40.  
  41. #ifndef LATTICE
  42.     if (SysBase->LibNode.lib_Version < 36) {
  43. #else
  44.     {
  45. #endif
  46.     char buf[64];
  47.  
  48.     sprintf(buf, "ENV:%s", envname);
  49.     return(ExtArgsFile(ac, pav, buf));
  50.     }
  51. #ifndef LATTICE
  52.       else {
  53.     str = malloc(1024);
  54.     len = GetVar(envname, str, 1024, 0);
  55.     if (len > 0)
  56.         str = strcpy(malloc(len + 1), str);
  57.     else
  58.         free(str);
  59.     return(ExtArgsBuf(ac, pav, str, len));
  60.     }
  61. #endif
  62. }
  63.  
  64. #else
  65.  
  66. int
  67. ExtArgsEnv(short ac, char ***pav, char *envname)
  68. {
  69.     char *str;
  70.     short len;
  71.  
  72.     if ((str = getenv(envname)) != NULL) {
  73.     len = strlen(str);
  74.     str = strcpy(malloc(len + 1), str);
  75.     return(ExtArgsBuf(ac, pav, str, len));
  76.     }
  77.     return(ac);
  78. }
  79.  
  80. #endif
  81.  
  82. int
  83. ExtArgsFile(short ac, char ***pav, char *file)
  84. {
  85.     int fd;
  86.     long len = -1;
  87.     char *str = NULL;
  88.  
  89.     if ((fd = open(file, O_RDONLY)) >= 0) {
  90.     if ((len = lseek(fd, 0L, 2)) > 0) {
  91.         str = malloc(len + 1);
  92.  
  93.         lseek(fd, 0L, 0);
  94.         read(fd, str, len);
  95.         str[len] = 0;
  96.     }
  97.     close(fd);
  98.     }
  99.     return(ExtArgsBuf(ac, pav, str, len));
  100. }
  101.  
  102. int
  103. ExtArgsBuf(short ac, char ***pav, char *str, long len)
  104. {
  105.     char *ptr;
  106.     int nac = 0;
  107.     char **nav;
  108.  
  109.     if (len < 0)
  110.     return(ac);
  111.  
  112.     /*
  113.      *    parse
  114.      */
  115.  
  116.     ptr = skipspace(str);
  117.     while (*ptr) {
  118.     ++nac;
  119.     ptr = skipnspace(ptr);
  120.     ptr = skipspace(ptr);
  121.     }
  122.     nav = malloc((ac + nac + 1) * sizeof(char *));
  123.     movmem(*pav, nav, ac * sizeof(char *));
  124.     nac = ac;
  125.     ptr = skipspace(str);
  126.     while (*ptr) {
  127.     nav[nac] = ptr;
  128.     ptr = skipnspace(ptr);
  129.     if (*ptr)
  130.         *ptr++ = 0;
  131.     ptr = skipspace(ptr);
  132.     ++nac;
  133.     }
  134.     nav[nac] = NULL;
  135.     ac = nac;
  136.     *pav = nav;
  137.     return(ac);
  138. }
  139.  
  140. char *
  141. skipspace(ptr)
  142. char *ptr;
  143. {
  144.     while (*ptr == '\n' || *ptr == ' ' || *ptr == 9)
  145.     ++ptr;
  146.     return(ptr);
  147. }
  148.  
  149. char *
  150. skipnspace(ptr)
  151. char *ptr;
  152. {
  153.     while (*ptr != '\n' && *ptr != ' ' && *ptr != 9 && *ptr)
  154.     ++ptr;
  155.     return(ptr);
  156. }
  157.  
  158. /*
  159.  *  check for path existance
  160.  */
  161.  
  162. void
  163. CreateObjPath(file)
  164. char *file;
  165. {
  166.     short i;
  167.     short j;
  168. #ifdef AMIGA
  169.     BPTR lock;
  170. #else
  171.     struct stat statBuf;
  172. #endif
  173.     char tmp[128];
  174.  
  175.     for (i = strlen(file); i >= 0 && file[i] != '/' && file[i] != ':'; --i);
  176.  
  177.     if (i <= 0)
  178.     return;
  179.     strncpy(tmp, file, i);
  180.     tmp[i] = 0;
  181.  
  182.     /*
  183.      *    valid directory
  184.      */
  185.  
  186. #ifdef AMIGA
  187.     if (lock = Lock(tmp, SHARED_LOCK)) {
  188.     UnLock(lock);
  189.     return;
  190.     }
  191. #else
  192.     if (stat(tmp, &statBuf) == 0)
  193.     return;
  194. #endif
  195.  
  196.     /*
  197.      *    invalid, attempt to create directory path.
  198.      */
  199.  
  200.     for (j = 0; j <= i; ++j) {
  201.     if (file[j] == '/') {
  202.         strncpy(tmp, file, j);
  203.         tmp[j] = 0;
  204. #ifdef AMIGA
  205.         if (mkdir(tmp) < 0 && errno != EEXIST)
  206.         break;
  207. #else
  208.         if (mkdir(tmp, 0666) < 0 && errno != EEXIST)
  209.         break;
  210. #endif
  211.     }
  212.     }
  213. }
  214.  
  215.